var services = angular.module('services',[]);
var directives = angular.module('directives', ['services']);
var myApp = angular.module('myApp', ['services','directives','ngRoute','ngMessages','ngSanitize','ngCookies','pasvaz.bindonce','darthwade.loading','notifications','angularFileUpload']);

console.log('rout test')

myApp.config(['$locationProvider','$routeProvider', function($locationProvider,$routeProvider) {
    $locationProvider.hashPrefix('!');
    $routeProvider.when('/data', {
        templateUrl: 'public/views/pages/data/data.html',
        controller: 'DataCtrl'
    })
    .when('/table', {
        templateUrl: 'public/views/pages/table/table.html',
        controller: 'TableCtrl'
    })
    .when('/charts', {
        templateUrl: 'public/views/pages/charts/charts.html',
        controller: 'ChartsCtrl'
    })
    .when('/share', {
        templateUrl: 'public/views/pages/share/share.html',
        controller: 'ShareCtrl'
    })
    .when('/login', {
        templateUrl: 'public/views/pages/login/login.html',
        controller: 'LoginCtrl'
    })
    .when('/reg', {
        templateUrl: 'public/views/pages/reg/reg.html',
        controller: 'RegCtrl'
    })
    .when('/setting', {
        templateUrl: 'public/views/pages/setting/setting.html',
        controller: 'SetCtrl'
    })
    .when('/industry', {
        templateUrl: 'public/views/pages/industry/industry.html',
        controller: 'IndustryCtrl'
    })
    .when('/forget', {
        templateUrl: 'public/views/pages/forget/forget.html',
        controller: 'ForgetCtrl'
    })
    .otherwise({ redirectTo: '/login' });
}]);

myApp.run(['$route', '$templateCache', '$http', (function($route, $templateCache, $http) {
    var url;
    for (var i in $route.routes) {
        if ($route.routes[i].preload) {
            if (url = $route.routes[i].templateUrl) {
                $http.get(url, { cache: $templateCache });
            }
        }
    }
})]);

//user
myApp.run(['$rootScope', '$location', 'Session', function ($rootScope, $location, Session) {
    $rootScope.$on('$routeChangeStart', function (next, current) {
        console.log(Session);
        // 用户是否已经登录过 首屏进入
        if(Session.UserInfo && !current.$$route){
            console.log('userInfo');
            console.log(Session.UserInfo);
            // $('#NavCtrl').show();
            $location.path('/share');
            return;
        }
        // 用户未登录过 首屏进入
        if(!Session.UserInfo && !current.$$route){
            console.log('userInfo');
            console.log(Session.UserInfo);
            // $('#NavCtrl').hide();
            $location.path('/login');
            return;
        }

        //用户未登录 路由跳转  注册，登录.找回密码页面
        if(!Session.UserInfo && current.$$route){          
            // $('#NavCtrl').hide();
            var path = current.$$route.originalPath;
            //不是reg，login，forget页面
            if( path != '/reg' && path != '/login' && path != '/forget'){
                $location.path('/login');
            }
            // $location.path('/login');
            return;
        }


        //用户登录 路由跳转 注册，登录页面不可用
        if(Session.UserInfo && current.$$route){
            // $('#NavCtrl').show();
            var path = current.$$route.originalPath;
            if(path == '/reg' || path == '/login'){
             $location.path('/share');
         }
     }
 });
}]);


myApp.controller('NavCtrl', ['$scope','$location','Session','$timeout','$rootScope','tools', function ($scope,$location,Session,$timeout,$rootScope,tools) {

    $(".mySlider").sideNav();
    $scope.mobile = false;
    if($(window).width()<=414){
        $scope.mobile = true;
    }else{
        $scope.mobile = false;
    }

    console.log('NavCtrl is loading....');


    //nav route active
    $scope.$on('$routeChangeSuccess',function(){
        $scope.page = $location.path().replace('/','');
    });




    
    $scope.hideNav = function(){
        $(".mySlider").sideNav('hide');
    };
    // User Login Out
    $scope.loginOut =function(){
        // $(".button-collapse,#my-slider").sideNav();
        $('.mySlider').sideNav('destroy');
        
        tools.$get({
            url:'proxy/marketconditions/user/userLoginOut.htm'
        }).then(function(resp){
            Session.destroy();
            $scope.userInfo = null;
            $timeout(function(){
                $location.path('/login');
            },50);
        },function(resp){

        });
        
    }

    
    // Session User Login
    $scope.userInfo = Session.UserInfo;
    var clearFun = $rootScope.$on('LOGIN_SUCCESS_EV', function(){
        console.log('login success!')
        $scope.userInfo = Session.UserInfo;
    });
    var clearFun2 =$rootScope.$on('LOGIN_EV', function(){
        $scope.userInfo = Session.UserInfo;
        $timeout(function(){
            console.log('slider login')
            $(".mySlider").sideNav();
        },10);

    });
    $scope.$on('$destroy',function(){
        clearFun();
        clearFun2();
    });

}]);

$(document).ready(function(){
    $(".mySlider").sideNav();
});